package com.yjh.server.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yjh.pojo.entity.ReservationMain;
import com.yjh.pojo.vo.R;
import com.yjh.pojo.vo.ReserveVo;
import com.yjh.server.service.ReservationMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/admin/verify")
public class VerifyController {
    @Autowired
    private ReservationMainService reservationMainService;

    @PutMapping("/verifyReservation")
    public R<String> verifyReservation(@RequestParam("reserveId")String reserveId){
        System.out.println(reserveId);
//        Integer id=(Integer)reserveId;
//        reservationMainService.
        int id = Integer.parseInt(reserveId);
//        判断是否在有效期内，判断是否到可核销时间，判断日期
        ReservationMain reservationMain = reservationMainService.getById(id);

        if(reservationMain.getReserveStatus()!=0){
            return R.error("核销码已无效");
        }

        LocalDate date = LocalDate.now();
        if(!reservationMain.getReserveDate().equals(date)){
            return R.error("不可提前核销");
        }


        LambdaUpdateWrapper<ReservationMain> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ReservationMain::getReserveId,id);
        wrapper.set(ReservationMain::getReserveStatus,1);

        reservationMainService.update(wrapper);

        return R.success("核销成功");
    }

    @GetMapping("/getReservationById")
    public R<ReserveVo> getReservationById(@RequestParam("reserveId")Integer reserveId){

        ReserveVo reserveVo = reservationMainService.getReservationById(reserveId);

        return R.success(reserveVo);
    }
}
